let errorImgHtml = '<img class="err-img" src="../staticFile/images/pic12.gif" width="16" height="16"/>';
$(function(){
    $('#loginName').blur(function(){
        if (isNull(V('loginName'))){
            showNameErrorMsg('请输入登录名');
        }
    }).focus(function(){
        $('#nameErrorMsg').hide();
        $('#loginName').css({'border':''});
    });
    $('#btnNextWriteName').click(function(){
        if (isNull(V('loginName'))){
            showNameErrorMsg('请输入手机号');
            return;
        }
        this.disabled = true;
        verifyUserPhone(this);
    });
    $('#btnSendCheckCode').click(function () {
		//60秒倒计时
    	let time = 60, phoneObj = this;
  		phoneObj.disabled = true;
  		let checkCodeInterval = window.setInterval(function () {
  			time --;
  			if (time < 1) {
				resetSendBtn(checkCodeInterval,phoneObj);
			}else {
				$(phoneObj).val(time + '秒后可发送');
			}
		},1000);
  		//获取验证码
  		goSendCheckCode(checkCodeInterval,phoneObj);
    });
    $('#btnSureModify').click(function() {
    	this.disabled = true;
    	sureModifyPwd(this);
	});
});

/**
 * 重置验证码发送按钮
 * @param checkCodeInterval 定时器
 * @param sendBtn 按钮对象
 */
function resetSendBtn(checkCodeInterval,sendBtn) {
	window.clearInterval(checkCodeInterval);
	$(sendBtn).val('发送验证码');
	sendBtn.disabled = false;
}
function showNameErrorMsg(errorMsg){
    $('#nameErrorMsg').show().html(errorImgHtml + errorMsg);
    $('#loginName').css({'border':'1px #ff6749 solid'});
}
/**
 * 验证用户手机号是否存在
 */
function verifyUserPhone(btnObj) {
	//从后台判断用户名是否存在
	let fpLoginName = V('loginName');
	if (!phoneNumberIsRight(fpLoginName)){
		showNameErrorMsg('手机号码错误');
		btnObj.disabled = false;
		return;
	}
    $.ajax({
		type: "POST",
		url: '/index/user/isExistsPhone',
		data: {phone : fpLoginName},
		dataType: 'json',
		async: false,
		success: function(verifyResult) {
			btnObj.disabled = false;
			if (verifyResult) {
				let userPhone = fpLoginName.substring(0, 3) + "*****" + fpLoginName.substring(8, 11);
				$('#verifyLoginPhone').text(userPhone);
				$('#verifyName').hide();
				//$('#verifyPhone').show();
				$('.verify').show();
			} else {
				showNameErrorMsg('用户名不存在或已被冻结');
			}
		},
		error: function() {
			btnObj.disabled = false;
			showNameErrorMsg('系统繁忙，请稍候再试');
		}
	});
}
//获取验证码
function goSendCheckCode(checkCodeInterval,sendBtn) {
    let pvErr = $('#passwordErrorMsg');
    $.ajax({
        type: "POST",
        url: '/index/user/sendCodeByNoLogin',
        data:{phone: V('loginName')},
        dataType: 'json',
        async: false,
        success: function(verifyResult) {
        	let vs = verifyResult.status,errMsg = '当前系统繁忙,请稍后再获取验证码';
        	if(vs == '1'){
				errMsg = '验证码已发送';
        	}else if(vs == '3'){
                errMsg = '该手机号码未注册，请重新填写手机号';
        	}else if(vs == '4'){
                errMsg = '您当前IP地址获取验证码次数过多，请稍候再试';
        	}else if(vs == '5'){
                errMsg = '您当前手机号码获取验证码次数过多，请稍候再试';
        	}else if(vs == '6'){
				errMsg = '手机号码格式不对';
			}
        	if (vs != '1'){
				resetSendBtn(checkCodeInterval,sendBtn);
			}
			pvErr.html((vs == '1' ? '' : errorImgHtml) + errMsg);
        },
        error: function() {
            pvErr.html(errorImgHtml + '当前系统繁忙,请稍后再获取验证码');
		}
    });
}
/**
 * 确认修改密码
 */
function sureModifyPwd(btnSureObj) {
	let modifyPwdCheckCode = V('modifyPwdCheckCode'),newPwd = V('newPwd'), sureNewPwd = V('sureNewPwd');
	if (isNull(modifyPwdCheckCode)) {
		alert('验证码不能为空');
		btnSureObj.disabled = false;
		return;
	}
	if (isNull(newPwd)) {
		alert('新密码不能为空');
		btnSureObj.disabled = false;
		return;
	}
	if (isNull(sureNewPwd)) {
		alert('确认新密码不能为空');
		btnSureObj.disabled = false;
		return;
	}
	if (newPwd != sureNewPwd) {
		alert('新密码和确认密码不相等');
		btnSureObj.disabled = false;
		return;
	}
	if(!confirm('您确认要修改密码吗？')){
		btnSureObj.disabled = false;
		return;
	}
	let shaObj = new jsSHA("SHA-1", "TEXT");
	shaObj.update(newPwd);
	newPwd = shaObj.getHash("HEX");
	$.ajax({
		type: "POST",
		url: '/index/user/modifyPwdByCodeWithNoLogin',
		data:{myPhone: V('loginName'),checkCode: V('modifyPwdCheckCode'),newPwd: newPwd},
		dataType: 'json',
		success: function(modifyResult) {
			let ms = modifyResult.status;
			if(ms == '1'){
				alert('修改密码时出错');
			}else if(ms == '2'){
				alert('未获取到验证码');
			}else if(ms == '3'){
				alert('未获取到手机号');
			}else if(ms == '4'){
				alert('未获取到用户密码');
			}else if(ms == '5'){
				alert('验证码错误或已失效');
			}else if(ms == '0'){
				alert('密码修改成功,快去登录吧');
				window.location.href = 'userLogin.html';
			}else {
				alert('密码修改失败');
			}
			btnSureObj.disabled = false;
		},
		error: function() {
			alert('当前系统繁忙,请稍后再获取验证码');
			btnSureObj.disabled = false;
		}
	});
}